﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using BLToolkit.Data;
using BLToolkit.DataAccess;
using BLToolkit.Data.Linq;
using BLToolkit.EditableObjects;
using BLToolkit.Reflection;
using BLToolkit.Mapping;

namespace Clipping.Utils
{
	/// <summary>
	/// 数据库实体类的基础类，主要是封装了CUD操作
	/// </summary>
	/// <typeparam name="T"></typeparam>
	public class ModelBase<T>
		where T : class
	{
		/// <summary>
		/// 当前实体类的表中所有的对象，用于访问表中所有的实体
		/// </summary>
		public static Table<T> Records
		{
			get
			{
				//初始化DBHelper
				RuntimeHelpers.RunClassConstructor(typeof(T).TypeHandle);

				System.Diagnostics.Debug.Assert(DBConnection.DefaultConnection.ThreadId == System.Threading.Thread.CurrentThread.ManagedThreadId);

				return DBConnection.DefaultConnection.GetTable<T>();
			}
		}

		#region CreateInstance

		public static T Create()
		{
			return TypeAccessor.CreateInstanceEx<T>();
		}

		public static T1 Create<T1>()
		{
			return TypeAccessor.CreateInstanceEx<T1>();
		}

		#endregion

		/// <summary>
		/// 插入当前数据实体对象到数据库中
		/// </summary>
		/// <returns>受影响记录的条数（1）</returns>
		public virtual int Insert()
		{
			System.Diagnostics.Debug.Assert(DBConnection.DefaultConnection.ThreadId == System.Threading.Thread.CurrentThread.ManagedThreadId);

			return DBConnection.DefaultConnection.Insert(this as T);
		}

		/// <summary>
		/// 插入当前数据实体对象到数据库中
		/// </summary>
		/// <returns>受影响记录的条数（1）</returns>
		public virtual int Insert(DBConnection conn)
		{
			return conn.Insert(this as T);
		}

		/// <summary>
		/// 插入当前数据实体对象到数据库中
		/// </summary>
		/// <returns>新插入记录的自动增长字段的值（Identity）</returns>
		public virtual int InsertWithIdentity()
		{
			System.Diagnostics.Debug.Assert(DBConnection.DefaultConnection.ThreadId == System.Threading.Thread.CurrentThread.ManagedThreadId);

			return Convert.ToInt32(DBConnection.DefaultConnection.InsertWithIdentity(this as T));
		}

		/// <summary>
		/// 按照关键字更新数据库中的当前数据实体
		/// 注意：此方法不能更新关键字
		/// </summary>
		/// <returns></returns>
		public virtual int Update()
		{
			System.Diagnostics.Debug.Assert(DBConnection.DefaultConnection.ThreadId == System.Threading.Thread.CurrentThread.ManagedThreadId);

			return DBConnection.DefaultConnection.Update(this as T);
		}

		/// <summary>
		/// 按照关键字更新数据库中的当前数据实体
		/// 注意：此方法不能更新关键字
		/// </summary>
		/// <returns></returns>
		public virtual int Update(DBConnection conn)
		{
			return conn.Update(this as T);
		}

		/// <summary>
		/// 删除数据库中的当前数据实体
		/// </summary>
		/// <returns></returns>
		public virtual int Delete()
		{
			System.Diagnostics.Debug.Assert(DBConnection.DefaultConnection.ThreadId == System.Threading.Thread.CurrentThread.ManagedThreadId);

			return DBConnection.DefaultConnection.Delete(this as T);
		}

		/// <summary>
		/// 删除数据库中的当前数据实体
		/// </summary>
		/// <returns></returns>
		public virtual int Delete(DBConnection conn)
		{
			return conn.Delete(this as T);
		}
	}
}
